package com.alks.function.controller.web;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.ImageUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.CutYield;
import com.alks.entity.data.entity.StatementsInvoice;
import com.alks.entity.data.entity.stitch.StitchMonthVo;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.service.web.statements.StatementsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:hxl
 * @create: 2024-11-28 14:39
 * @Description:
 */
@ApiRestController("/statements")
@Api(tags = {"报表汇总", "开发报表", "按交期分析", "收款汇总查询", "按品牌分析", "计划达成", "每周计划","每日计划"})
public class StatementsController {
    @Autowired
    StatementsService statementsService;

    //信用证汇总查询
    @GetMapping("/dHFinanceCreditDetail")
    @ApiOperation(value = "信用证汇总查询", tags = "报表汇总")
    @WebLogger
    public ResponseInfo dHFinanceCreditDetail(@RequestParam(required = false) String creditNo,
                                              @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                              @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                              @RequestParam Integer pageSize,
                                              @RequestParam Integer pageNum) {
        return statementsService.dHFinanceCreditDetail(creditNo, startDate, endDate, pageNum, pageSize);
    }

    //信用证号导出
    @PostMapping("/downloadDhFinanceCreditDetail")
    @ApiOperation(value = "信用证号导出", tags = "报表汇总")
    @WebLogger
    public void downloadDhFinanceCreditDetail(HttpServletResponse response, @RequestBody StatementsInvoice result) throws IOException {
        statementsService.downloadDhFinanceCreditDetail(response, result);
    }

    @PostMapping("/getDhFinanceDetail")
    @ApiOperation(value = "信用证号导出", tags = "报表汇总")
    @WebLogger
    public ResponseInfo getDhFinanceDetail(@RequestBody StatementsInvoice result) {
        return statementsService.getDhFinanceDetail(result);
    }

    @PostMapping("/getDhReceiptHead")
    @ApiOperation(value = "第一个页面查询", tags = "收款汇总查询")
    @WebLogger
    public ResponseInfo getDhReceiptHead(@RequestParam(required = false) String receiptNo,
                                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                                         @RequestParam(required = false) String custom,
                                         @RequestParam Integer pageSize,
                                         @RequestParam Integer pageNum) {
        return statementsService.getDhReceiptHead(receiptNo, startTime, endTime, custom, pageNum, pageSize);
    }

    @PostMapping("/downloadDhReceiptHead")
    @ApiOperation(value = "第一个页面导出", tags = "收款汇总查询")
    @WebLogger
    public void downloadDhReceiptHead(HttpServletResponse response, @RequestParam(required = false) String receiptNo,
                                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                                      @RequestParam(required = false) String custom) throws IOException {
        statementsService.downloadDhReceiptHead(response, receiptNo, startTime, endTime, custom);
    }

    @PostMapping("/getDhReceiptDetail")
    @ApiOperation(value = "第二个页面查询", tags = "收款汇总查询")
    @WebLogger
    public ResponseInfo getDhReceiptDetail(@RequestParam String receiptNo) {
        return statementsService.getDhReceiptDetail(receiptNo);
    }

    @PostMapping("/downloadDhReceiptDetail")
    @ApiOperation(value = "第二个页面导出", tags = "收款汇总查询")
    @WebLogger
    public void downloadDhReceiptDetail(HttpServletResponse response, String receiptNo) throws IOException {
        statementsService.downloadDhReceiptDetail(response, receiptNo);
    }

    @GetMapping("/getStitchMonthProd")
    @ApiOperation(value = "针车月产量", tags = "开发报表")
    @WebLogger
    public ResponseInfo getStitchMonthProd(@RequestParam @ApiParam(value = "年月例如 202411") String month, @RequestParam @ApiParam("10： 针车 3：裁剪") String prodSeq) {
        return statementsService.getStitchMonthProd(month, prodSeq);
    }

    //板房、配套、备料月产量
    @GetMapping("/getOtherMonthProd")
    @ApiOperation(value = "板房、配套、备料月产量", tags = "开发报表")
    @WebLogger
    public ResponseInfo getOtherMonthProd(@RequestParam @ApiParam(value = "年月例如 202411") String month, @RequestParam @ApiParam("1： 板房 2：备料 配套： 8") String prodSeq) {
        return statementsService.getOtherMonthProd(month, prodSeq);
    }

    @GetMapping("/getCurrentMonth")
    @ApiOperation(value = "获取该员工生产数据", tags = "开发报表")
    @WebLogger
    public ResponseInfo getCurrentMonth(@RequestParam String workerId, @RequestParam @ApiParam(value = "如果是总计传年月 当天传年月日 例如20241011") String month, @RequestParam @ApiParam(value = "1:总计日期 2：天数日期") String choose, @RequestParam @ApiParam("10： 针车 3：裁剪") String prodSeq) {
        return statementsService.getCurrentMonth(workerId, month, choose, prodSeq);
    }

    //针车月产量导出
    @PostMapping("/downloadStitchMonthProd")
    @ApiOperation(value = "针车月产量导出", tags = "开发报表")
    @WebLogger
    public void downloadStitchMonthProd(HttpServletResponse response, @RequestParam @ApiParam(value = "年月例如 202411") String month, @RequestParam @ApiParam("10： 针车 3：裁剪") String prodSeq) throws IOException {
        ResponseInfo res = this.statementsService.getStitchMonthProd(month, prodSeq);
        Map<String, Object> map = (Map<String, Object>) res.getData();
        List<StitchMonthVo> list = (List<StitchMonthVo>) map.get("list");
        Map<String, Object> stitch = (Map<String, Object>) map.get("stitch");
        ExcelUtils.creatByTemplate(response, "yp_month_prod_stitch.xlsx", "yp_month_prod_stitch.xlsx", list, stitch);
    }

    //员工生产汇总表导出
    @PostMapping("/downloadStitchMonthProdTotal")
    @ApiOperation(value = "员工生产汇总表导出", tags = "开发报表")
    @WebLogger
    public void downloadStitchMonthProdTotal(HttpServletResponse response
            , @RequestParam String workerId
            , @RequestParam @ApiParam(value = "如果是总计传年月 当天传年月日 例如20241011") String month
            , @RequestParam @ApiParam(value = "1:总计日期 2：天数日期") String choose
            , @RequestParam String workerName
            , @RequestParam @ApiParam("10： 针车 3：裁剪") String prodSeq) throws IOException {
        ResponseInfo res = statementsService.getCurrentMonth(workerId, month, choose, prodSeq);
        List<CutYield> list = (List<CutYield>) res.getData();
        BigDecimal total = new BigDecimal("0");
        for (CutYield cutYield : list) {
            BigDecimal prodQty = cutYield.getProdQty() == null ? BigDecimal.ZERO : cutYield.getProdQty();
            total = total.add(prodQty);
            try {
                cutYield.setUrl(new URL(cutYield.getPhotoPath()));
            } catch (Exception e) {
                cutYield.setUrl(null);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("workerName", workerName + month);
        ExcelUtils.creatByTemplate(response, "yp_month_prod_stitch_d.xlsx", "yp_month_prod_stitch_d.xlsx", list, map);
    }

    //开发月报表
    @PostMapping("/prodMonthProd")
    @ApiOperation(value = "月产量统计", tags = "开发报表")
    @WebLogger
    public ResponseInfo prodMonthProd(@RequestParam @ApiParam(value = "年份例如 2024") String month) {
        return statementsService.prodMonthProd(month);
    }

    //月产量统计板房备料
    @PostMapping("/prodMonthProdDetail")
    @ApiOperation(value = "月产量统计板房备料", tags = "开发报表")
    @WebLogger
    public ResponseInfo prodMonthProdDetail(@RequestParam @ApiParam(value = "年月例如 2024-11") String month, @ApiParam("10： 针车 3：裁剪") @RequestParam String prodSeq) {
        return statementsService.prodMonthProdDetail(month, prodSeq);
    }

    //日产量统计
    @PostMapping("/prodDayProd")
    @ApiOperation(value = "日产量统计", tags = "开发报表")
    @WebLogger
    public ResponseInfo prodDayProd(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, @RequestParam String prodSeq) {
        return statementsService.prodDayProd(startDate, endDate, prodSeq);
    }

    //按年份查询
    @PostMapping("/prodDayProdByYear")
    @ApiOperation(value = "按年份查询", tags = "按交期分析")
    @WebLogger
    public ResponseInfo prodDayProdByYear(@RequestParam @ApiParam(value = "年份例如 2025") String year) {
        return statementsService.prodDayProdByYear(year);
    }

    //按品牌分析
    @PostMapping("/prodDayProdByBrand")
    @ApiOperation(value = "点击日期、按品牌分析", tags = "按交期分析")
    @WebLogger
    public ResponseInfo prodDayProdByBrand(@RequestParam @ApiParam(value = "点击日期传年份例如 202501，按品牌分析传 2025") String month) {
        return statementsService.prodDayProdByBrand(month);
    }

    //按品牌分析
    @PostMapping("/prodDayProdByBrandDetail")
    @ApiOperation(value = "点击各流程节点未完成", tags = "按交期分析")
    @WebLogger
    public ResponseInfo prodDayProdByBrandDetail(@RequestParam @ApiParam(value = "年份例如 202501") String month,
                                                 @RequestParam @ApiParam(value = "1:板房2：备料3：裁断8：配套10：针车12：成型") String prodSeq) {
        return statementsService.prodDayProdByBrandDetail(month, prodSeq);
    }

    @PostMapping("/analysisByBrand")
    @ApiOperation(value = "点击品牌", tags = "按品牌分析")
    @WebLogger
    public ResponseInfo analysisByBrand(@RequestParam @ApiParam(value = "品牌") String brand) {
        return statementsService.analysisByBrand(brand);
    }

    @PostMapping("/analysisByBrandDetail")
    @ApiOperation(value = "点击各流程节点未完成", tags = "按品牌分析")
    @WebLogger
    public ResponseInfo getUnfinishedByBrand(@RequestParam @ApiParam(value = "品牌") String brand, @RequestParam String prodSeq, @RequestParam String month, @RequestParam String season) {
        return statementsService.getUnfinishedByBrand(brand, season, prodSeq, month);
    }


    @PostMapping("/getCompletedByMonth")
    @ApiOperation(value = "按月、周、日查询", tags = "计划达成")
    @WebLogger
    public ResponseInfo getCompletedByMonth(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return statementsService.getCompletedByMonth(startDate, endDate);
    }

    @PostMapping("/getWeekPlan")
    @ApiOperation(value = "备料、裁剪、工艺、配套、针车、大底、成型周计划", tags = "每周计划")
    @WebLogger
    public ResponseInfo getWeekPlan(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                    @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                    @RequestParam @ApiParam(value = "1:备料2：裁剪3：工艺4：配套5：针车6：大底7：成型") String choose,
                                    @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
        return statementsService.getWeekPlan(startDate, endDate, choose, pageNum, pageSize);
    }

    @PostMapping("/getDayPlan")
    @ApiOperation(value = "备料、裁剪、工艺、配套、针车、大底、成型每日计划", tags = "每日计划")
    @WebLogger
    public ResponseInfo getDayPlan(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date day,@RequestParam @ApiParam(value = "1:备料2：裁剪3：工艺4：配套5：针车6：大底7：成型") String choose) {
        return statementsService.getDayPlan(day,choose);
    }
}
